package com.gitee.cui.client.handler;

import com.gitee.cui.common.common.bean.msg.ProtoMsg;
import io.netty.channel.ChannelHandler;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

/**
 * @author cuiqingqiang
 * @version 1.0
 * @date 2021/10/27 16:42
 **/
@Slf4j
@ChannelHandler.Sharable
@Service
public class ChatMsgHandler extends ChannelInboundHandlerAdapter {
    @Override
    public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
        // 判断消息类型
        if (!(msg instanceof ProtoMsg.Message)) {
            super.channelRead(ctx, msg);
            return;
        }

        // 判断消息类型
        ProtoMsg.Message message = (ProtoMsg.Message) msg;
        ProtoMsg.HeadType type = message.getType();
        if (ProtoMsg.HeadType.MESSAGE_REQUEST != type) {
            super.channelRead(ctx, msg);
            return;
        }

        ProtoMsg.MessageRequest request = message.getMessageRequest();
        String content = request.getContent();
        String from = request.getFrom();
        System.out.printf("收到消息【%s】，from uid = %s%n", content, from);
    }
}
